package cn.lee.cplibrary.util.takephotos;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import cn.lee.cplibrary.init.CPInitializer;

/**
 * 图片的base64数据处理
 */
public class ImageBase64 {
    private static void checkPermit() {
        CPInitializer.checkInitialization();
    }  /**
     * 将图片转换成Base64编码的字符串
     *
     * @param path:默认jpg
     * @return base64编码的字符串
     */
    public static String imageToBase64(final String path, final Activity context) {
        checkPermit();
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        //先进行图片压缩
//        String outPath = ImageUtils.getFileName(path, new File(path).getName(), ImageUtils.generateImgePath(context));
        String outPath = path;
        InputStream is = null;
        byte[] data = null;
        String result = "";
        try {
            is = new FileInputStream(outPath);
            //创建一个字符流大小的数组。
            data = new byte[is.available()];
            //写入数组
            is.read(data);
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data, Base64.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    /**
     * base64编码字符集转化成图片文件。
     *
     * @param base64Str
     * @param path      文件存储路径
     * @return 是否成功
     */
    public static boolean base64ToFile(String base64Str, String path) {
        checkPermit();
        byte[] data = Base64.decode(base64Str, Base64.DEFAULT);
        for (int i = 0; i < data.length; i++) {
            if (data[i] < 0) {
                //调整异常数据
                data[i] += 256;
            }
        }
        OutputStream os = null;
        try {
            os = new FileOutputStream(path);
            os.write(data);
            os.flush();
            os.close();
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 　　* 将base64转换成bitmap图片
     * 　　* @param string base64字符串
     * 　　* @return bitmap
     */

    public static Bitmap stringToBitmap(String string) {
        checkPermit();

        // 将字符串转换成Bitmap类型

        Bitmap bitmap = null;

        try {

            byte[] bitmapArray;

            bitmapArray = Base64.decode(string, Base64.DEFAULT);

            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,

                    bitmapArray.length);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return bitmap;

    }

    public static String getMimeType(String base64String) {
        checkPermit();

        String mimeType="image/webp";//默认
        if (base64String == null || base64String.isEmpty()) {
            return mimeType;
        }

        // Remove the data URL scheme prefix if present
        if (base64String.startsWith("data:image/")) {
            int commaIndex = base64String.indexOf(',');
            if (commaIndex > 0) {
                mimeType = base64String.substring(5, commaIndex);
            }
        }
        byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
        if (bytes.length >= 8) {
            if (bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xD8 && bytes[2] == (byte) 0xFF) {
                mimeType="image/jpeg";
            } else if (bytes[0] == (byte) 0x89 && bytes[1] == (byte) 0x50 && bytes[2] == (byte) 0x4E && bytes[3] == (byte) 0x47) {
                mimeType= "image/png";
            } else if (bytes[0] == (byte) 0x47 && bytes[1] == (byte) 0x49 && bytes[2] == (byte) 0x46 && bytes[3] == (byte) 0x38) {
                mimeType= "image/gif";
            } else if (bytes[0] == (byte) 0x52 && bytes[1] == (byte) 0x49 && bytes[2] == (byte) 0x46 && bytes[3] == (byte) 0x46) {
                mimeType= "image/webp";
            }
        }

        return mimeType; // Unknown format
    }
}
